package com.xigua._树;

import com.xigua.TreeNode;

/**
 * @author LiRongHua
 * @Title: ${file_name}
 * @Package ${package_name}
 * @Description: ${todo}
 * @date 2022/4/1816:56
 */
public class _938_二叉搜索树的范围和 {
    int sum = 0;
    public int rangeSumBST(TreeNode root, int low, int high) {
        dfs(root,low,high);
        return sum;
    }

    void dfs(TreeNode root, int low, int high){
        if (root == null ) return;
        if (root.val >= low && root.val <=high){
            sum+=root.val;
            dfs(root.left,low,high);
            dfs(root.right,low,high);
        }else if (root.val < low){
            dfs(root.right,low,high);
        }else if (root.val > high){
            dfs(root.left,low,high);
        }
    }


}
